/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package com.mtons.mblog.modules.service.impl;

import com.mtons.mblog.modules.repository.PostRepository;
import com.mtons.mblog.modules.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author langhsu
 */
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private PostRepository postRepository;

    @Override
    public Map<String, Integer> findAllTagGroup() {
        List<Object[]> allTagGroup = postRepository.findAllTagGroup();
        Map<String, Integer> map = new HashMap<>();
        for (Object[] objects : allTagGroup) {
            //标签
            String tags = objects[0].toString();
            //数量
            int count = Integer.parseInt(objects[1].toString());
            String[] split = tags.split(",");
            //判断map中是否已经存储该标签
            for (String s : split) {
                if (map.containsKey(s)) {
                    //通过键获取值  键是tags，值是count
                    Integer step = map.get(s);
                    //如果已经存储该字符，则把出现的次数加count
                    map.put(s, step + count);
                } else {
                    map.put(s, count);
                }
            }
        }
        return map;
    }
}
